<?php
/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2023/12/26
 * Time: 14:48
 */

namespace App\Http\Controllers\API\LifeCycle\Package;

use App\Http\Controllers\Controller;
use App\Http\Resources\PackageOperatorCollection;
use App\Http\Resources\PackageOperatorDetailResource;
use App\Http\Resources\PackageOperatorResource;
use App\Models\CarPackageOrderAttr;
use App\Models\CarPackageUsedLog;
use App\Services\LifeCycle\CarService;
use App\Services\LifeCycle\OrderService;
use App\Services\LifeCycle\PackageService;
use App\Services\LifeCycle\StoreService;
use App\Services\LifeCycle\UsedService;
use App\Services\LifeCycle\UserService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class OperatorController extends Controller
{
    protected $model = CarPackageUsedLog::class;
    protected $collection = PackageOperatorCollection::class;
    protected $resource = PackageOperatorResource::class;

    /**
     * @param Request $request
     */
    public function operatorList(Request $request)
    {
        $param = $request->all();
        $user = Auth::user();
        $where = [];

        $keywords = $param['keywords'] ?? '';
        $operator_at = $param['operator_at'];

        if ($keywords != '') {
            $where[] = ['attr_title', 'like', "%{$keywords}%"];
        }

        $operator_at_start = date('Y-m-d H:i:s', strtotime("2023-01-01 00:00:00"));
        $operator_at_end = date('Y-m-d H:i:s', time());

        if ($operator_at != '' && $operator_at != '[]') {
            $operator_at = explode(',', $operator_at);
            $operator_at_start = $operator_at[0];
            $operator_at_end = $operator_at[1];
        }

        $orm = $this->model::where('operator_user_id', $user->id)
            ->where($where)
            ->whereDate('operator_at', '>=', $operator_at_start)
            ->whereDate('operator_at', '<=', $operator_at_end)
            ->orderBy('id', 'desc');

        $per_page = 10;

        $list = $orm->paginate($per_page);

        $data = new $this->collection($list);

        return $this->success($data);
    }

    public function detail(Request $request)
    {
        $used = new PackageOperatorDetailResource(UsedService::operatorDetail($request->get('used_id')));

        return $this->success($used);
    }

    public function editDetail(Request $request)
    {
        $param = $request->all();

        if (isset($param['used_id'])) {
            $result = new PackageOperatorDetailResource(UsedService::operatorDetail($request->get('used_id')));
            $order_id = $result->order_id;
            $user_id = $result->user_id;
        } else {
            $result = OrderService::detail($param['order_id']);
            $order_id = $result->id;
            $user_id = $result->user_id;
        }

        $attr = UsedService::attrSelect($order_id, $user_id);

        return $this->success(['used' => $result, 'attr' => $attr]);
    }

    public function packageOrder(Request $request)
    {
        $user = Auth::user();

        if ($user->type != MEMBER_TYPE_STAFF) {
            return $this->failure('无权限访问！');
        }

        if ($user->package_attr_permission_ids == '[]') {
            return $this->failure('无权限访问！');
        }

        $order_id = $request->get('order_id');

        $order = OrderService::detail($order_id);

        if (!$order) {
            return $this->failure('未查询到相关信息！');
        }

        if ($order->status != ORDER_STATUS_PAID) {
            return $this->failure('该订单已失效！');
        }

        if (date('Y-m-d H:i:s') > date("Y-m-d 23:59:59",strtotime("+1years", strtotime($order->created_at)))) {
            return $this->failure('该套餐已过期！');
        }

        $user_id = $order->user_id;

        $user = UserService::userInfo($user_id);
        $car = CarService::one($order->user_car_id);
        $attr = OrderService::attr($order_id, $user_id);
        $package = PackageService::one($order->package_id);

        if ($package->id == PACKAGE_ID_BJQ) {
            $car = '';
        }

        return $this->success([
            'user' => $user,
            'car' => $car,
            'attr' => $attr,
            'package' => $package,
            'order' => $order
        ]);
    }

    public function create(Request $request)
    {
        $result = UsedService::create($request->all());

        if ($result['code'] == 200) {
            return $this->success();
        } else {
            return $this->failure($result['message']);
        }
    }

    public function edit(Request $request)
    {
       UsedService::edit($request->all());

       return $this->success();
    }
}